<%--
  @author Duminda Dharmakeerthi
  @author Supun Nimesh Karunathilaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript">
    //todo : include in common constant
    var page=1;
    var previousPage=0;

    $(function() {
        initData();
        $('select#gnDivisionId').bind('change', function(evt1) {
            page=1;
            initData();

        });
        page=1;
        hideNextPre();

    });



    function hideNextPre(){
         $('#previousId').hide();
        $('#nextId').hide();
    }

    function showNextPre(){
         $('#previousId').show();
        $('#nextId').show();
    }

    function initData() {
        var id = $('select#gnDivisionId').attr('value');
        //var mode = "byGNDivision";
        var mode = "byGNDivisionLimit";
        var type = "detail";
        if (id != 0) {
             if($("input#userGroup").val() == "orip" && id == -1){
                  id = $('select#dsDivision').attr('value');
                  mode = "byDSDivisionLimit";
                  type = "detail";
            }
            $.getJSON('${pageContext.request.contextPath}/common/LandLookupService', {id:id, mode:mode, type:type, pageNo:page}, function(data) {

                if(data.landList.length > 0){
                    loadLandList(data.landList);
                    showNextPre();
                    if(page==1){
                       $('#previousId').hide();
                    }
                    if(data.landList.length < parseInt($('#maxRows').val())){
                        $('#nextId').hide()
                    }
                }else{
                    //page=0;
                    page=previousPage;
                    loadLandList(data.landList);
                     $('#nextId').hide();
                }
            });
        }
    }

    function next() {
        previousPage=page+1;
        page++;
        initData();
    }

    function previous() {

        if(page<=1){
           page=1;

        }else{
            page--;
        }

        initData();
    }

    function confirmDelete() {
        var agree = confirm("Are you sure you want to delete this land?");
        return agree;
    }

    function loadLandList(landInfo) {
        var opt = '';
        opt += '<thead>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#ref').val() + '</th>' +
                '<th class="left-align">' + $('#lname').val() + '</th>' +
                '<th class="center-align">' + $('#plan').val() + '</th>' +
                '<th class="center-align">' + $('#lot').val() + '</th>' +
                '<th class="center-align">' + $('#extent').val() + '</th>' +
                '<th class="center-align">' + $('#action').val() + '</th>' +
                '</tr>' +
                '</thead>';
        opt += '<tbody>';
        if (landInfo.length > 0) {
            for (var i = 0; i < landInfo.length; i++) {
                var reference = (landInfo[i].referenceNumber > 0) ? landInfo[i].referenceNumber : $('#undefined').val();
                var extent = roundNumber(landInfo[i].landExtent, 4);
                opt += '<tr>' +
                        '<td class="center-align">' + landInfo[i].id + '</td>' +
                        '<td>' + landInfo[i].nameOfTheLand + '</td>' +
                        '<td class="center-align">' + landInfo[i].planNumber + '</td>' +
                        '<td class="center-align">' + landInfo[i].lotNumber + '</td>' +
                        '<td class="center-align">' + extent + '</td>' +
                        '<td class="center-align">';

                if (landInfo[i].initialRecordId == 0) {
                    opt += '<a title="' + $('#add_detail_tooltip').val() + '" href="loadLandDetails.do?landId=' + landInfo[i].id + '"> <img src="${pageContext.request.contextPath}/images/detailed-form.png" style="border:none; width:25px; height:25px;"/> </a>';
                } else {
                    //  opt += '<input type="button" value="Attach Kml" onClick="callAndroidApp(\''+ landInfo[i].id +'\')" />';
                    // opt += '<a title="KML" href="loadKMLForm.do?landId=' + landInfo[i].id + '"> <img src="${pageContext.request.contextPath}/images/registration-form.png" style="border:none; width:25px; height:25px;"/> </a>';
                    opt += '<a title="' + $('#edit_tooltip').val() + '" href="loadLandDetails.do?editMode=1&landId=' + landInfo[i].id + '"> <img src="${pageContext.request.contextPath}/images/registration-form.png" style="border:none; width:25px; height:25px;"/> </a>';
                    /*if(landInfo[i].approvalState == 'REJECTED'){
                     opt += '<a tooltip="Edit" href="editDetailLandInformation.do?landId=' + landInfo[i].id + '"> <img src="${pageContext.request.contextPath}/images/reject.png" style="border:none; width:25px; height:25px;"/> </a>';

                     }else{
                     opt += '<a tooltip="Edit" href="editDetailLandInformation.do?landId=' + landInfo[i].id + '"> <img src="${pageContext.request.contextPath}/images/registration-form.png" style="border:none; width:25px; height:25px;"/> </a>';
                     }*/

                }
                // opt += '<a title="Delete" href="deleteDetailLandInformation.do?landId=' + landInfo[i].id + '" onClick="return confirmDelete()"><img src="${pageContext.request.contextPath}/images/reject.png" style="border:none; width:25px; height:25px;"/></a>';
                opt += '</td></tr>';
            }
        }
        opt += '</tbody>';
        $('.land-table').html(opt);
        $('.land-table').dataTable({
            aaSorting: [],
            "sPaginationType": "full_numbers",
            "bDestroy": true,
            "oLanguage": {
                "sZeroRecords": $('#no-record').val(),
                "sLengthMenu": $('#display').val() + " _MENU_ " + $('#records').val(),
                "sSearch": $('#search').val(),
                "sInfo": $('#showing').val() + " _START_ " + $('#to').val() + " _END_ " + $('#of').val() + " _TOTAL_ " + $('#entries').val(),
                "sInfoEmpty": $('#no-record').val(),
                "oPaginate": {
                    "sFirst": $('#first').val(),
                    "sLast": $('#last').val(),
                    "sNext": $('#next').val(),
                    "sPrevious": $('#previous').val()
                }
            },
            "fnDrawCallback": function() {
                if ($('.dataTables_paginate span span.paginate_button').size()) {
                    $('.dataTables_paginate')[0].style.display = "block";
                } else {
                    $('.dataTables_paginate')[0].style.display = "none";
                }
            }
        });
    }
    function roundNumber(number, decimals) { // Arguments: number to round, number of decimal places
        var newnumber = new Number(number + '').toFixed(parseInt(decimals));
        return parseFloat(newnumber);
    }

    $(function() {
                $('select#district').bind('change', function(evt1) {
                    var id = $("select#district").attr("value");
                    $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id:id,mode:2},
                             function(data) {
                                 //var options1 = '<option value="0"> - Select  - </option> ';
                                 var options1 = '<option value="0">' + $('#select').val()  + '</option> ';
                                 var ds = data.dsDivisionList;
                                 for (var i = 0; i < ds.length; i++) {
                                     options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                                 }
                                 $("select#dsDivision").html(options1);
                                 var options2 = '<option value="0">' + $('#select').val()  + '</option> ';
                                 $("select#gnDivisionId").html(options2);
                             });
                });
            });

            $(function() {
                $('select#dsDivision').bind('change', function(evt1) {
                    var id = $("select#dsDivision").attr("value");
                    $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id:id,mode:3},
                             function(data) {
                                 //var options1 = '<option value="0"> - Select  - </option> ';
                                 var options1 = '<option value="0">' + $('#select').val()  + '</option> ';
                                     options1 += '<option value="-1">' + $('#all').val()  + '</option> ' ;
                                 var ds = data.gnDivisionList;
                                 for (var i = 0; i < ds.length; i++) {
                                     options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                                 }
                                 $("select#gnDivisionId").html(options1);
                             });

                });
            });

</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="landRegistrationModule.do">
        <span><img src="${pageContext.request.contextPath}/images/land-reg.png" alt="Land Registration">
            <s:label value="%{getText('land_registration.label')}"/></span>
    </s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="detailForm.do">
        <span><img src="${pageContext.request.contextPath}/images/registration-form.png" alt="Enter Detailed Land Information">
        <s:label value="%{getText('enter_detail_land_info.label')}"/></span>
    </s:a>
    <div id="back-button" align="right">
            <s:form><s:submit value="%{getText('back_btn.label')}" action="landRegistrationModule"/></s:form>
    </div>
</div>
<div id="detail-land-update-list">
    <div class="page-title">
        <h2><s:label value="%{getText('enter_detail_land_info.label')}"/></h2>
    </div>
    <s:if test="hasActionErrors() || hasActionMessages()">
        <div id="message-box" class="message-box">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </div>
    </s:if>
    <table class="width-500-px margin-10-auto">

        <%--<s:if test="#session.userGroup=='orip'">
            <tr>
            <td class="left-align"><s:label value="%{getText('district.label')}"/></td>
            <td><s:select list="districtList" name="districtName" headerKey="0" headerValue="%{getText('select.label')}"
                          id="district"/></td>

            </tr>
            <tr>
            <td class="left-align"><s:label value="%{getText('ds_division.label')}"/></td>
            <td><s:select name="dsDivisionName" id="dsDivision" headerKey="0" headerValue="%{getText('select.label')}"
                          list="dsDivisionList" cssClass="width-180-px"/>
            </td>
            </tr>
            <tr>
                <td><s:label value="%{getText('gn_division.label')}"/></td>
                <td><s:select id="gnDivisionId" name="gnDivisionId" headerKey="0" headerValue="%{getText('select.label')}"
                              list="gnDivisionList" cssClass="width-200-px"/></td>
            </tr>
        </s:if>
        <s:else>
                    <tr>
                        <td><s:label value="%{getText('district.label')}"/></td>
                        <td><s:textfield id="districtId" name="districtName" cssClass="width-200-px" disabled="true"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('ds_division.label')}"/></td>
                        <td><s:textfield id="dsDivisionId" name="dsDivisionName" cssClass="width-200-px" disabled="true"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('gn_division.label')}"/></td>
                            <td><s:select id="gnDivisionId" name="gnDivisionId" headerKey="0" headerValue="%{getText('select.label')}"
                                      list="gnDivisionList" cssClass="width-200-px"/></td>
                    </tr>
        </s:else>--%>

    </table>
    <div id="land-list">
        <table class="width-100 margin-10-auto table land-table">

        </table>

        <div id="nextPreId" align="right">
        <img onclick="previous();"  id="previousId" src="${pageContext.request.contextPath}/images/previous.gif" border="none"/>
        <img onclick="next();" id="nextId" src="${pageContext.request.contextPath}/images/next.gif" border="none"/>
        </div>

    </div>
</div>
<%--<fieldset style="border:none;">
    <div class="next-previous" style="float:right;margin-right:10px;clear:both;">
        <s:url id="previousUrl" action="SearchPrevious.do">
            <s:param name="pageNo" value="%{#request.pageNo}"/>
            <s:param name="dsDivisionId" value="#request.dsDivisionId"/>
        </s:url>
        <s:url id="nextUrl" action="SearchNext.do">
            <s:param name="pageNo" value="%{#request.pageNo}"/>
        </s:url>
        &lt;%&ndash;<s:if test="printStart!=0 & printStart>0">&ndash;%&gt;
        <s:if test="printStart!=0 & pageNo !=1">
            <s:a href="%{previousUrl}">
                <img src="<s:url value='/images/previous.gif'/>" border="none"/>
            </s:a>
            <s:label value="%{getText('previous.label')}"/>
        </s:if>
        <s:if test="SearchList.size >=50">
            <s:a href="%{nextUrl}">
                <img src="<s:url value='/images/next.gif'/>" border="none"/>
            </s:a>
            <s:label value="%{getText('next.label')}"/>
        </s:if>
    </div>
</fieldset>--%>

<%-- Localization text for java script generated text --%>
<%--<s:hidden id="ref" value="%{getText('reference_no.label')}"/>--%>
<s:hidden id="ref" value="%{getText('slmsSerialNumber.label')}"/>
<s:hidden id="lname" value="%{getText('name_of_the_land.label')}"/>
<s:hidden id="plan" value="%{getText('plan_no.label')}"/>
<s:hidden id="lot" value="%{getText('lot_no.label')}"/>
<s:hidden id="extent" value="%{getText('extent_perches.label')}"/>
<s:hidden id="action" value="%{getText('action.label')}"/>
<s:hidden id="reject_tooltip" value="%{getText('reject_tooltip')}"/>
<s:hidden id="add_detail_tooltip" value="%{getText('add_detail_tooltip')}"/>
<s:hidden id="edit_tooltip" value="%{getText('edit_tooltip')}"/>
<s:hidden id="undefined" value="%{getText('undefined.label')}"/>

<%-- Localization text for data tables --%>
<s:hidden id="display" value="%{getText('display.label')}"/>
<s:hidden id="records" value="%{getText('records.label')}"/>
<s:hidden id="search" value="%{getText('search.label')}"/>
<s:hidden id="no-record" value="%{getText('no_record.label')}"/>
<s:hidden id="showing" value="%{getText('showing.label')}"/>
<s:hidden id="to" value="%{getText('to.label')}"/>
<s:hidden id="of" value="%{getText('of.label')}"/>
<s:hidden id="entries" value="%{getText('entries.label')}"/>
<s:hidden id="first" value="%{getText('first.label')}"/>
<s:hidden id="last" value="%{getText('last.label')}"/>
<s:hidden id="next" value="%{getText('next.label')}"/>
<s:hidden id="previous" value="%{getText('previous.label')}"/>

<s:hidden id="maxRows" name="@lk.icta.common.AppConstants@MAX_ROWS_PER_PAGE_STRING"/>
<s:hidden id="select" value="%{getText('select.label')}"/>
<s:hidden id="all" value="%{getText('all.label')}"/>
<s:hidden id="userGroup" name="#session.userGroup"/>